import { fromJS } from 'immutable';
import { Utils } from '../../../lib';
import { LoginActionType } from '../const';
import { apiStatus, apiResultParser } from '../../../shared/util/reducerUtil';

const { createReducer } = Utils;

const { loginInfo } = LoginActionType.key;
const initialState = fromJS({
  /** 登录信息 */
  [loginInfo]: {
    status: apiStatus.init,
    message: '',
    data: {},
  },
});

const { LOGIN } = LoginActionType;
const handlers = {
  /** 执行登录 */
  [LOGIN]: (state, { payload, error, meta }) => {
    const apiState = state.get(loginInfo);
    const newApiState = apiResultParser(apiState, { payload, error, meta });
    return state.set(loginInfo, newApiState);
  },
};

export default createReducer(initialState, handlers);
